﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

public partial class ManageEmployees : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string libID = this.Request.QueryString.Get("id");
        lblLibID.Text = libID;
        lblConfirm.Text = "";
        
    }

    protected void btnAddNewPerson_Click(object sender, EventArgs e)
    {
        ManagePerson managePer = new ManagePerson();
        //SqlDataAdapter dataAdapt = managePer.enterNewPatron();

    }

    // Binds data to GridView1 when find person button is clicked
    protected void btnFindPerson_Click(object sender, EventArgs e)
    {
        //GridView1.DataSourceID = "SqlDataSource2";
        GridView1.DataBind();
        
    }

    //catches error when details view is updated and displays save confirmation message
    protected void DetailsView1_ItemUpdated(object sender, DetailsViewUpdatedEventArgs e)
    {
        if (e.Exception != null)
        {
            lblConfirm.Text = "A database error has occured. <br /><br /> Message: " + e.Exception.Message;
            e.ExceptionHandled = true;
            e.KeepInEditMode = true;
        }
        else if (e.AffectedRows == 0)
            lblConfirm.Text = "Another user may have updated that record. <br /> Please try again.";
        else
            lblConfirm.Text = "Your record has been saved";
            GridView1.DataBind();
    }

    //catches error when a new record is inserted in details view and displays save confirmation message
    protected void DetailsView_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
    {
        if (e.Exception != null)
        {
            //if (e.Exception.Message.ToString() == "Violation of PRIMARY KEY constraint 'PK_Person'. Cannot insert duplicate key in object 'dbo.PERSON'. Violation of PRIMARY KEY constraint 'PK_Employee'. Cannot insert duplicate key in object 'dbo.EMPLOYEE'. The statement has been terminated. The statement has been terminated.")
            //{
                lblConfirm.Text = "This Id number has already been used.";
                e.ExceptionHandled = true;
                e.KeepInInsertMode = true;
            //}
        }
        else if (e.AffectedRows == 0)
            lblConfirm.Text = "Another user may have updated that record. <br /> Please try again.";
        else
            lblConfirm.Text = "Your record has been saved";
        GridView1.DataBind();
    }

    //-- Confirmation lable for searching for a person
    protected void SqlDataSource2_Selected(object sender, SqlDataSourceStatusEventArgs e)
    {
        if (e.AffectedRows < 1)
            lblNoResults.Text = "There are no results that match your search.";
        else
            lblNoResults.Text = "";
    }

    protected void btnAddNew_Click(object sender, EventArgs e)
    {
        txtFindPerson.Text = "";
        lblNoResults.Text = "";
        DetailsView1.ChangeMode(DetailsViewMode.Insert);
    }

    //protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    //{
    //    //DetailsView1.ChangeMode(DetailsViewMode.Edit);
    //    //lblNew.Text = "Edit Selected Record";
    //    //DetailsView1.DataBind();
        
    //}
    //protected void DetailsView1_PageIndexChanging(object sender, DetailsViewPageEventArgs e)
    //{
    //    //lblNew.Text = "Enter New Person";
    //}
    

    //protected void DetailsView1_OnInsert(object sender, DetailsViewCommandEventArgs e)
    //{ 
    //    //<script type='text/javascript'>alert('Person Added Successfully');
    //}

    //
    //protected void rbAccess_DataBinding(object sender, EventArgs e)
    //{
    //    RadioButtonList rbList = (RadioButtonList)sender;
    //    bool Librarian = (bool)DataBinder.Eval(rbList.BindingContainer, "DataItem.Librarian");
    //    string text = "Volunteer";

    //    if (Librarian)
    //        text = "Librarian";

    //    foreach (ListItem li in rbList.Items)
    //    {
    //        if (li.Text == text)
    //        {
    //            li.Selected = true;
    //            break;
    //        }
    //    }
    //}

}
